package com.tanhua.sso.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.tanhua.sso.mapper.UserMapper;
import com.tanhua.sso.pojo.User;
import com.tanhua.sso.service.SmsService;
import com.tanhua.sso.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * @author ZJWzxy
 * @date 2021/04/07
 */
@RestController
@RequestMapping("user")
@Slf4j
public class SmsController {

    @Autowired
    private SmsService smsService;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    /**
     * 发送短信验证码接口
     *
     * @param param 参数
     * @return 返回值
     */
    @PostMapping("login")
    public ResponseEntity<ErrorResult> sendCheckCode(@RequestBody Map<String, String> param) {
        ErrorResult errorResult = null;
        //从前端得到输入的手机号
        String phone = param.get("phone");

        //从redis中查询对应的信息  在发送短信验证用户是否被冻结
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        //获取手机号判断freezeRange状态
        queryWrapper.eq("mobile", phone);
        User user = this.userMapper.selectOne(queryWrapper);
        String redisFreezesKey = "Freezing_" + user.getId();
        if (this.redisTemplate.hasKey(redisFreezesKey)) {
            String freezeRange = this.redisTemplate.opsForValue().get(redisFreezesKey);
            //比较当前状态
            if (Integer.valueOf(freezeRange) == 1) {
                errorResult = ErrorResult.builder().errCode("000001").errMessage("账号已被冻结!").build();
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
            }
        } else {
            //帮他解冻
            LambdaUpdateWrapper<User> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper
                    .eq(User::getId, user.getId()) //修改的userId
                    .set(User::getUpdated, new Date()) //更新修改时间
                    .set(User::getFrozenTime, 0)//置空冻结状态种种
                    .set(User::getFrozenRange, 0)
                    .set(User::getFrozenReason, null)
                    .set(User::getFrozenRemark, null)
                    .set(User::getThawedReason, "冻结过期了"); //添加解冻状态
            userMapper.update(null, lambdaUpdateWrapper);
        }


        try {
            //调用SmsService中的sendCheckCode方法的的得到传入手机号的结果
            errorResult = this.smsService.sendCheckCode(phone);
            if (null == errorResult) {
                //发送验证码成功
                String msg = "验证码发送成功";
                log.info(msg);
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            //失败
            log.error("发送验证码失败~ phone" + phone, e);
            errorResult = ErrorResult.builder().errCode("000002").errMessage("短信验证码发送失败!").build();
        }
        //向前端返回失败结果信息
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }
}

